package com.iam.security;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * create by yongli on 2021-09-23 17:17
 */

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 应该是这些授权用户存储在内存中
        auth.inMemoryAuthentication().withUser("li").password("123").roles("admin").and()
                .withUser("ll").password("121").roles("dba");
    }


    /**
     * 使用这个配置的方法，还是有点太细致
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 对特定资源（路径）进行角色授权. 比如Security
        http.authorizeRequests().antMatchers("/user/**").hasRole("admin")
                // 所有资源都需要授权
                .anyRequest()
                .authenticated()
                .and()

                .formLogin()
                .loginProcessingUrl("/url")
                .permitAll()
                .and()
                .csrf()
                .disable();
    }
}
